package servletAction;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DAOContact;

/**
 * Servlet implementation class RemoveContact
 */
public class RemoveContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RemoveContact() {
        super();
        // TODO Auto-generated constructor stub
    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");


		if(id == "" ){
			response.sendRedirect("removeContact.jsp");
			return;
		}
		try{
			long id_int = Integer.parseInt(id);
			DAOContact dao = new DAOContact();
			if(dao.removeContact(id_int)){
				RequestDispatcher rd = request.getRequestDispatcher("/menu.jsp");
				rd.forward(request, response);
			}else{
				response.sendRedirect("removeContact.jsp");
				return;
			}
		}catch (Exception e) { 
			response.sendRedirect("removeContact.jsp");
			return;
		}
	}

}
